#!/bin/bash

export QEMU_SET_ENV=LD_PRELOAD=$TARGET_LD_PRELOAD

INFO=$(file $1)

echo "$INFO" | grep -q "ARM aarch64"
if [[ "$?" -eq "0" ]]; then
    export QEMU_LD_PREFIX=/usr/aarch64-linux-gnu
    exec qemu-aarch64 "$@"
fi

echo "$INFO" | grep -q "ARM, EABI"
if [[ "$?" -eq "0" ]]; then
    export QEMU_LD_PREFIX=/usr/arm-linux-gnueabihf
    exec qemu-arm "$@"
fi

echo "$INFO" | grep -q "MIPS64"
if [[ "$?" -eq "0" ]]; then
    export QEMU_LD_PREFIX=/usr/mips64-linux-gnuabi64
    exec qemu-mips64 "$@"
fi

echo "$INFO" | grep -q "x86-64"
if [[ "$?" -eq "0" ]]; then
    export QEMU_LD_PREFIX=/
    exec qemu-x86_64 "$@"
fi

echo "ERROR: Unknown architecture!"
exit 1
